
###############################################################################
# The communication module (communication.py)
###############################################################################
import socket
import struct

def send(channel, msg):
    buf = msg
    value = len(buf)
    size = struct.pack("L",value)
    channel.send(size)
    channel.send(buf)

def receive(channel):

    size = struct.calcsize("L")
    size = channel.recv(size)
    try:
        size = struct.unpack("L", size)[0]
    except struct.error, e:
        return ''
    
    buf = ""

    while len(buf) < size:
        buf = channel.recv(size - len(buf))

	return buf
